package demo.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.CacheName;

import demo.interceptor.DemoInterceptor;
import demo.model.User;
import demo.service.UserService;
import demo.validator.UserValidator;

// 事务支持 
// 声明式事务只能在Controller层
@Before(value={Tx.class, DemoInterceptor.class})
public class HelloController extends Controller {
	
	public static final User dao = new User();
	
	int i = 1;
	
	UserService service = new UserService();
	
	public void index() {
		System.out.println(this.getPara());
		//renderText("Hello JFinal World.");
		System.out.println("controller" + i++);
		//this.render("/my.jsp");// 加了/采用绝对路径
		//this.render("my");// 未加采用相对路径
		this.renderHtml("<h2>html</h2>");
	}
	
	public void m() {
		System.out.println(this.getPara());
		if(this.getPara() != null) {
			this.setAttr("requestError", "e1");
			this.setSessionAttr("sessionError", "e2");
			//this.redirect("/error.jsp");
			//this.forwardAction("/error.jsp");
			this.render("/error.jsp");
			
			return;
		}
		this.renderHtml("<h2>html</h2>");
	}
	
	public void m2() {
		System.out.println(this.getPara(0));// url中的参数
		System.out.println(this.getPara(1));
		
		System.out.println(this.getPara("name"));// ?后面的参数
		
		this.renderText("jf");
	}
	
	public void add() {
		String name = this.getPara(0);
		String pwd = this.getPara(1);
		dao.add(name, pwd);
		//int i = 1/0;
		//dao.findById(38).set("password", "pwd").update();
		
		// 通过删除缓存来获得实时的值,否则会根据配置的ehcache缓存策略来进行
		CacheKit.remove("sampleCache1", "key");
		this.renderText("add");
	}
	
	public void findByCache() {
		// 根据ehcache的名称以及所定义的key来缓存结果集
		List<User> users = dao.findByCache("sampleCache1abc", "key", "select * from user");
		System.out.println(users);
		
		/*List<User> users2 = dao.findByCache("sampleCache1", "key", "select * from user");
		System.out.println(users2);*/
		renderJson(users);
	}
	
	public void findByCache2() {
		// 根据ehcache的名称以及所定义的key来缓存结果集
		List<User> users = dao.findByCache("sampleCache1", "key", "select * from user");
		System.out.println(users);
		
		/*List<User> users2 = dao.findByCache("sampleCache1", "key", "select * from user");
		System.out.println(users2);*/
		renderJson(users);
	}
	
	// action层的缓存,要在ehcache.xml中配置名称为/hello/world/list的缓存
	// Could not find cache config [/hello/world/list], using default.
	// 也可以用@CacheName注解指定缓存名称
	// Could not find cache config [userlist], using default.
	@Before(CacheInterceptor.class)
	@CacheName("userlist")
	public void list() {
		List<User> users = dao.find("select * from user");
		this.setAttr("users", users);
		//this.render("users.html");
		renderJson(users);
	}
	
	@Before(UserValidator.class)
	public void delete() {
		int id = Integer.parseInt(this.getPara("id"));
		service.delete(id );
	}
}